package com.loveyukari.controller;

import com.loveyukari.model.Address;
import com.loveyukari.model.Auth;
import com.loveyukari.model.AuthRegister;
import com.loveyukari.model.ExchangeGfit;
import com.loveyukari.service.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/admin/score")
@Controller
public class AuthScoreController {

    @Autowired
    private ManagerService managerService;
    @Autowired
    private NewRegisterService newRegisterService;
    @Autowired
    private AuthScoreService authScoreService;
    @Autowired
    private AuthService authService;
    @Autowired
    private ExchangeService exchangeService;
    @Autowired
    private AddressService addressService;

    /**
     * 积分列表
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String authList(Model model){
        List<JSONObject> list = authScoreService.getAuthScoreList();
        model.addAttribute("list",list);
        return "score/list";
    }


    /**
     * 房管礼物列表
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/exchange/list")
    public String exchangeList(HttpServletRequest request, Model model){
        String sign =(String) request.getAttribute("sign");
        int authId = managerService.getManageAuthId(sign);
        int score = 0;
        Auth auth = authService.getCacheAuth(authId);
        if (auth != null){
            score = auth.getScore();
        }
        List<JSONObject> list = newRegisterService.getAuthRegisterList(authId);
        int punctual = 0;
        int late = 0;
        int absenteeism = 0;
        for (JSONObject json:
               list ) {
            int status = json.getInt("status");
            switch (status){
                case -1:absenteeism++;break;
                case 0:punctual++;break;
                case 1:late++;break;
            }
        }
        List<ExchangeGfit> gfitList = exchangeService.getGfitOnline();
        int price = exchangeService.getAuthPrice(authId);

        List<Address> addressList = addressService.getAuthAddress(authId);

        model.addAttribute("list",list);
        model.addAttribute("gfitList",gfitList);
        model.addAttribute("addressList",addressList);
        model.addAttribute("total",list.size());
        model.addAttribute("punctual",punctual);
        model.addAttribute("late",late);
        model.addAttribute("absenteeism",absenteeism);
        model.addAttribute("authScore",score);
        model.addAttribute("price",price);
        return "score/gfit_list";
    }


}
